package com.job.action;

import java.math.BigDecimal;
import java.util.Set;

import org.apache.struts2.ServletActionContext;
import org.springframework.beans.factory.annotation.Autowired;

import com.job.model.Forder;
import com.job.model.Product;
import com.job.model.Sorder;
import com.job.model.User;
import com.job.service.ProductService;
import com.job.service.SorderService;
import com.job.service.UserService;
import com.opensymphony.xwork2.ActionSupport;

public class SorderAction extends ActionSupport {

	private Product product;
	@Autowired
	private ProductService productService;
	@Autowired
	private UserService userService;
	@Autowired
	private SorderService sorderService;

	public String tianjia() {
		product = productService.get(product.getId());
		User user = (User) ServletActionContext.getRequest().getSession().getAttribute("u");
		user = userService.get(user.getId());

		sorderService.save(product, user);

		BigDecimal b = new BigDecimal(0);
		int num = 0;
		Set<Forder> newForders = user.getForders();
		for (Forder f : newForders) {
			b = b.add(f.getTotal());
			Set<Sorder> sorders = f.getSorders();
			for (Sorder s : sorders) {
				num += s.getNumber();
			}
		}
		ServletActionContext.getRequest().getSession().setAttribute("num", num);
		ServletActionContext.getRequest().getSession().setAttribute("price", b);
		return "success";

	}

	public Product getProduct() {
		return product;
	}

	public void setProduct(Product product) {
		this.product = product;
	}

	public ProductService getProductService() {
		return productService;
	}

	public void setProductService(ProductService productService) {
		this.productService = productService;
	}

	public UserService getUserService() {
		return userService;
	}

	public void setUserService(UserService userService) {
		this.userService = userService;
	}

	public SorderService getSorderService() {
		return sorderService;
	}

	public void setSorderService(SorderService sorderService) {
		this.sorderService = sorderService;
	}

}
